package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Role;
import com.itheima.domain.Users;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        req.getSession().setAttribute("username", username);
//        PrintWriter out = resp.getWriter();
//        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
//            out.flush();
//            out.println("<script>");
//            out.println("alert('用户名或密码不能为空，请重新输入！');");
//            out.println("history.back();");
//            out.println("</script>");
//            req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
//        }
//        Users users = userService.login(username, password);
//        req.getSession().setAttribute("loginUser", users);
//        if (users == null) {
//            out.flush();
//            out.println("<script>");
//            out.println("alert('用户名或密码错误，请重新输入！');");
//            /*out.println("history.back();");*/
//            out.println("</script>");
//            req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
//        } else {
//            if ("1".equals(req.getParameter("ck"))) {
//                Cookie unCookie = new Cookie("username", username);
//                Cookie pwdCookie = new Cookie("password", password);
//                //设置Cookie的有效期为10天
//                unCookie.setMaxAge(60 * 60 * 24 * 10);
//                pwdCookie.setMaxAge(60 * 60 * 24 * 10);
//                unCookie.setPath("/");
//                pwdCookie.setPath("/");
//                resp.addCookie(unCookie);
//                resp.addCookie(pwdCookie);
//            }
//            req.getSession().setAttribute("user", users);
//            req.getRequestDispatcher("/pages/main.jsp").forward(req, resp);
//        }
//    }
//      获取名字和密码
        String ck = req.getParameter("ck");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Users user = userService.login(username, password);
//        if ("1".equals(ck)) {
//            Users user1 = (Users) req.getSession().getAttribute("loginUser");
//            Cookie unCookie = new Cookie("username", user.getUsername());
//            Cookie pwdCookie = new Cookie("password", user.getPassword());
////           设置Cookie的有效期为10天
//            unCookie.setMaxAge(60 * 60 * 24 * 10);
//            pwdCookie.setMaxAge(60 * 60 * 24 * 10);
//            unCookie.setPath("/");
//            pwdCookie.setPath("/");
//            resp.addCookie(unCookie);
//            resp.addCookie(pwdCookie);
//        }
//        req.getRequestDispatcher("/pages/main.jsp").forward(req,resp);
        if (user != null) {
            req.getSession().setAttribute("loginUser", user);
            req.getSession().setAttribute("username", username);
            req.getRequestDispatcher("/pages/main.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
        }
    }


/*    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
//        调用方法获取所有信息
        List<Users> list = userService.findAll(pageNum, pageSize);

//        使用分页插件
        PageInfo<Users> pageInfo = new PageInfo<>(list);

//        放入会话域
        req.setAttribute("page", pageInfo);

//        重定向到list,jsp页面
        req.getRequestDispatcher("/pages/user-list.jsp").forward(req, resp);
    }*/

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.getSession().removeAttribute("loginUser");
        req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
    }

    private void changePassword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Users user = (Users) req.getSession().getAttribute("loginUser");
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
//
//        Users users = BeanUtil.fillBean(req, Users.class);

        userService.changePassword(user);

        req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
    }

    //跳转方法
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/user-update.jsp").forward(req, resp);
    }

    private void rememberUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ck = req.getParameter("ck");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if ("1".equals(ck)) {
            Users user = (Users) req.getSession().getAttribute("loginUser");
            Cookie unCookie = new Cookie("username", user.getUsername());
            Cookie pwdCookie = new Cookie("password", user.getPassword());
//           设置Cookie的有效期为10天
            unCookie.setMaxAge(60 * 60 * 24 * 10);
            pwdCookie.setMaxAge(60 * 60 * 24 * 10);
            unCookie.setPath("/");
            pwdCookie.setPath("/");
            resp.addCookie(unCookie);
            resp.addCookie(pwdCookie);
        }
    }


    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前页码 每条显示的条目数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        List<Users> list = userService.findAll(pageNum, pageSize);
        //分页插件处理分页信息
        PageInfo<Users> PageInfo = new PageInfo<Users>(list);
        //存入作用域
        req.setAttribute("page", PageInfo);
        req.getRequestDispatcher("/pages/user-list.jsp").forward(req,resp);
    }

    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users userInfo = BeanUtil.fillBean(req, Users.class);
        userService.save(userInfo);
        findAll(req,resp);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("ids");
        if (id!= null) {
            userService.delete(id);
        }
        findAll(req,resp);
    }

    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (id!= null) {
            Users userInfo = userService.findById(id);
            if (userInfo != null) {
                List<Role> roles = userInfo.getRoles();
                req.setAttribute("roleList", roles);
                req.getRequestDispatcher("/pages/user-role-add.jsp").forward(req, resp);
            } else {
                findAll(req,resp);
            }

        }
    }
    protected void findLikeName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        if (name!= null) {
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            List<Users> list = userService.findLikeName(name,pageNum, pageSize);
            //分页插件处理分页信息
            PageInfo<Users> PageInfo = new PageInfo<Users>(list);
            req.setAttribute("page", PageInfo);
            req.getRequestDispatcher("/pages/user-list.jsp").forward(req,resp);
        }
    }



    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        status = status.equals("1") ? "0" : "1";
        Map map = new HashMap();
        map.put("id", id);
        map.put("status", status);
        userService.update(map);
        findAll(req,resp);
    }





    protected void findById1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (id!= null) {
            Users userInfo = userService.findById1(id);
            req.setAttribute("user",userInfo);
            req.getRequestDispatcher("/pages/user-detail.jsp").forward(req,resp);
        }

    }
    protected void update1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users userInfo = BeanUtil.fillBean(req, Users.class);

        userService.update1(userInfo);
        findAll(req,resp);
    }
}
